Read a date or lot code
Use this recipe when the inspection has to read characters printed on a product or its label, such as a best-before date, a lot code or a batch number, and check them against what should be printed. You can read them in two ways, and the first needs no training.
| OCR node | Your own text model | |
|---|---|---|
| What it needs | Nothing to train: its models are ready on the controller | A «Text Recognition» dataset, labeled with what each text says |
| When it fits | Start here | The OCR node keeps misreading your codes, even cropped and straightened |
| Guide | Read text with OCR | Label text for recognition |
Collect the images first
Both ways start with images of the codes as the line's camera sees them, uploaded to a dataset of type «Text Recognition». Cover every printer, light and dark backgrounds, slightly turned codes and the bad prints you need to catch. You will try the OCR node on these images and, if it is not enough, train your own model on them.
Try the OCR node
In a flow, the from-dataset node brings each dataset image in msg.image, which is where the OCR node reads (Send dataset images through a flow). Add an OCR node, keep «Task» on «OCR (Detection + Recognition)», and tick «Show debug image»: every line it reads is drawn on the image, with how sure it is. The answer, in msg.inference.ocr, holds text, with everything it read, and texts, with one entry per line, each with its text and a score from 0 to 1.
To help it read:
- Crop to the code. When the code is always in the same place, crop the image to it first with
rp-crop(Load and transform an image): less text to read and to choose from. - Straighten it. If products can arrive turned, switch on
use_doc_orientation_classifyin «Advanced settings», or straighten the image first with the «Document Rotation» task andrp-rotate. - Harder text. The «VLM (PaddleOCR-VL)» task uses a larger model that reads harder text, but more slowly.
If it reads the dataset's codes correctly, go straight to «Check the code in a flow» below.
Train your own reader
If it keeps misreading, label the images and train a model on your own print.
- Label. In the editor, choose «Draw Bounding Box», drag one box around each line of text, and type the text exactly as printed, with the same capitals, spaces and symbols. Each new box starts with the text of the previous one, which saves typing on a series of similar codes.
- Label faster. In text recognition datasets, Auto Labeling has PaddleOCR models that read the whole image or the boxes you draw («ROI»), so you only correct what they misread (Label automatically with a text prompt). A labeling assistant can also be trained for text (Choose the fastest way to label).
- Train. Start a «Production training» in «Models». It needs at least 10 labeled images (Train your first model). Reading a code takes two models: one that finds where the text is and one that reads it. The pre-labeling tool asks for one of each, «Detection model» and «Recognition model», to label new images (Pre-label images with a trained model).
- Read the results. Three scores measure a text model: «CAR», the share of characters read correctly; «Exact Match», the share of texts read exactly as labeled; and «WAR», the share of words read correctly. For a date or lot code, «Exact Match» is the score that counts, because one wrong character makes a wrong code.
On the line, the inferencer node runs the models you trained (Run a trained model on an image). A «Recognition» model answers with the text it read and its confidence, so give it the image already cropped to the code. Check its answer in the debug panel before you write the rule.
Check the code in a flow
Reading is only half the inspection. The rule then compares what was read with what should be printed, such as the lot of the batch now in production. Keep the expected code in the flow context, where a panel control can set it (Add buttons and selectors to a panel), and compare in a function node after the OCR node:
const expected = flow.get("lot");
const line = msg.inference.ocr.texts.find((t) => t.text === expected);
msg.payload = line && line.score >= 0.8 ? "OK" : "NOK";
msg.topic = msg.inference.ocr.text;
return msg;If no line matches, or the match is unsure, the product is NOK. To check only that a code of the right shape is present, search for it with a pattern, as Read text with OCR does for an eight-character lot code. A switch node then sends NOK products to the reject (Decide pass or fail from the predictions).
Show it to the operators
A «text» node (ui-text) on a panel shows msg.payload (Build your first inspection flow). Use one node for the verdict and another for the text that was read. The image canvas shows each image with a result card (Show inspection results on a panel). For a record of every product, rp-image-out saves each image named after its verdict, and rp-save-file saves what was read as JSON (Save images and results to disk).
Improve it
- Collect the misreads. Send the NOK images to the text recognition dataset with
dataset-upload. They arrive without boxes, so box them and type their text. They become the training data for your own reader, or for its next training (Collect images from a flow into a dataset). - Compare. Train again with «Copy from another training», compare «Exact Match» with the previous model, and choose the new model in the inferencer node (Retrain a model after adding images).